<?php

namespace App\Http\Controllers\Api;

use App\Constants\AdvConfigConstant;
use App\Constants\MiniProgramConstant;
use App\Models\CommonWords;
use App\Models\Config;
use App\Models\MiniProgram;
use App\Models\TemplateMessage;
use App\Modules\Health\Services\ConfigService;
use Illuminate\Http\Request;

class ConfigController extends Controller
{
    public function index(Request $request)
    {
        $name = $request->name;
        $info = Config::where(['name' => $name, 'mini_type' => $this->getMiniType()])->select(['name_title', 'content'])->first();

        return $this->success('success', $info ?: []);
    }

    /**
     * 配置信息
     * @return \Illuminate\Http\JsonResponse
     */
    public function config()
    {
        $memberId = auth('api')->user()->id ?? 0;

        if ($memberId) {
            $flag = isCloseAdv($memberId, $this->getMiniType());
        }

        $advConfig = (isset($flag) && $flag) ? [] : getMiniSettings($this->getMiniType(), AdvConfigConstant::ADV_GROUP);

        if ($advConfig) {
            $advSetting = $this->formatAdvSetting($advConfig);
        }

        $miniConfig = MiniProgram::where('slug', '=', $this->getMiniType())
            ->select(['slug', 'name'])
            ->first();

        $commonConfig = getMiniSettings($this->getMiniType(), MiniProgramConstant::GROUP);
        $commonConfig = [
            MiniProgramConstant::PUBLISH_TOPIC_SWITCH     => $commonConfig[MiniProgramConstant::PUBLISH_TOPIC_SWITCH] ?? 0,
            MiniProgramConstant::EXCHANGE_INTEGRAL_SWITCH => $commonConfig[MiniProgramConstant::EXCHANGE_INTEGRAL_SWITCH] ?? 0,
            MiniProgramConstant::VIP_RECHARGE_SWITCH      => $commonConfig[MiniProgramConstant::VIP_RECHARGE_SWITCH] ?? 0,
            MiniProgramConstant::KF_SWITCH                => $commonConfig[MiniProgramConstant::KF_SWITCH] ?? 0,
        ];


        return $this->_success([
            'adv_config'    => $advConfig,
            'adv_setting'   => $advSetting ?? [],
            'mini_info'     => $miniConfig,
            'common_config' => $commonConfig,
        ]);
    }

    public function templateInfo(Request $request)
    {
        $group = $request->input('group');
        if (!$group) return $this->error('参数错误');

        $list = TemplateMessage::where([
            'mini_type' => $this->getMiniType(),
            'group'     => $group,
            'status'    => 1,
        ])->pluck('template_id')->all();

        return $this->_success($list);
    }

    public function quickComment()
    {
        $list = CommonWords::where(['status' => true, 'mini_type' => $this->getMiniType()])
            ->orderByDesc('sort')
            ->select(['content', 'id'])
            ->get();

        return $this->_success(compact('list'));
    }

    protected function formatAdvSetting(array $setting)
    {
        $xwSetting = app(ConfigService::class)->getConfigs(\App\Modules\Health\Constants\AdvConfigConstant::GROUP);

        return [
            [
                'topic'                => [
                    'name'   => '帖子广告',
                    'status' => $setting[AdvConfigConstant::TOPIC_VIDEO_ADV_STATUS] ?? 0,
                    'num'    => $setting[AdvConfigConstant::TOPIC_VIDEO_ADV_INTERVAL] ?? 5,
                ],
                'topic_info'           => [
                    'name'   => '帖子内页广告',
                    'status' => $setting[AdvConfigConstant::TOPIC_INFO_ADV_STATUS] ?? 0,
                    'type'   => $setting[AdvConfigConstant::TOPIC_INFO_ADV_TYPE] ?? 0,
                ],
                'index'                => [
                    'name'   => '首页视频广告',
                    'status' => $setting[AdvConfigConstant::INDEX_VIDEO_ADV_STATUS] ?? 0,
                    'num'    => $setting[AdvConfigConstant::INDEX_VIDEO_ADV_INTERVAL] ?? 5,
                ],
                'comment'              => [
                    'name'   => '评论广告',
                    'status' => $setting[AdvConfigConstant::COMMENT_VIDEO_ADV_STATUS] ?? 0,
                    'num'    => $setting[AdvConfigConstant::COMMENT_VIDEO_ADV_INTERVAL] ?? 5,
                ],
                'theme'                => [
                    'name'   => '话题广告',
                    'status' => $setting[AdvConfigConstant::THEME_VIDEO_ADV_STATUS] ?? 0,
                    'num'    => $setting[AdvConfigConstant::THEME_VIDEO_ADV_INTERVAL] ?? 5,
                ],
                'waterfall'            => [
                    'name'   => '瀑布流原生模板广告',
                    'status' => $setting[AdvConfigConstant::WATERFALL_FLOW_STATUS] ?? 0,
                    'num'    => $setting[AdvConfigConstant::WATERFALL_FLOW_INTERVAL] ?? 5,
                ],
                'my'                   => [
                    'name'   => '个人中心Banner广告',
                    'status' => $setting[AdvConfigConstant::MY_BANNER_ADV_STATUS] ?? 0,
                    'type'   => $setting[AdvConfigConstant::MY_BANNER_ADV_TYPE] ?? 0,
                ],
                'integral_log'         => [
                    'name'   => '积分明细广告',
                    'status' => $setting[AdvConfigConstant::INTEGRAL_LOG_SCREEN_ADV_STATUS] ?? 0,
                    'type'   => $setting[AdvConfigConstant::INTEGRAL_LOG_SCREEN_ADV_TYPE] ?? 0,
                ],
                'integral_task'        => [
                    'name'   => '积分任务广告',
                    'status' => $setting[AdvConfigConstant::INTEGRAL_TASK_BANNER_ADV_STATUS] ?? 0,
                    'type'   => $setting[AdvConfigConstant::INTEGRAL_TASK_BANNER_ADV_TYPE] ?? 0,
                ],
                'sign'                 => [
                    'name'   => '签到广告',
                    'status' => $setting[AdvConfigConstant::SIGN_VIDEO_ADV_STATUS] ?? 0,
                    'type'   => $setting[AdvConfigConstant::SIGN_VIDEO_ADV_TYPE] ?? 0,
                ],
                'news_info'            => [
                    'name'   => '新闻中心内页广告',
                    'status' => $setting[AdvConfigConstant::NEWS_DETAILS_ADV_STATUS] ?? 0,
                    'type'   => $setting[AdvConfigConstant::NEWS_DETAILS_ADV_TYPE] ?? 0,
                ],
                'disease_discern_info' => [
                    'name'   => '病症鉴别内页广告',
                    'status' => $setting[AdvConfigConstant::DISEASE_DISCERN_DETAILS_ADV_STATUS] ?? 0,
                    'type'   => $setting[AdvConfigConstant::DISEASE_DISCERN_DETAILS_ADV_TYPE] ?? 0,
                ],
                'xw_video'             => [
                    'name'   => '穴位视频贴片广告',
                    'status' => $setting[AdvConfigConstant::PATCH_VIDEO_ADV_STATUS] ?? 0,
                    'type'   => $setting[AdvConfigConstant::PATCH_VIDEO_ADV_TYPE] ?? 0,
                ],
                'xw_info'              => [
                    'name'   => '穴位内页广告',
                    'status' => $setting[AdvConfigConstant::XW_INFO_ADV_STATUS] ?? 0,
                    'type'   => $setting[AdvConfigConstant::XW_INFO_ADV_TYPE] ?? 0,
                ],
                'disease_info'         => [
                    'name'   => '病症详情广告',
                    'status' => $xwSetting[\App\Modules\Health\Constants\AdvConfigConstant::DISEASE_STATUS] ?? 0,
                ],
                'breathe'              => [
                    'name'   => '爱呼吸列表广告',
                    'status' => $xwSetting[\App\Modules\Health\Constants\AdvConfigConstant::BREATHE_LIST_STATUS] ?? 0,
                ],
                'breathe_info'         => [
                    'name'   => '爱呼吸详情广告',
                    'status' => $xwSetting[\App\Modules\Health\Constants\AdvConfigConstant::BREATHE_STATUS] ?? 0,
                ],
                'tcm'                  => [
                    'name'   => '中医百科广告',
                    'status' => $setting[AdvConfigConstant::TCM_ADV_STATUS] ?? 0,
                    'type'   => $setting[AdvConfigConstant::TCM_ADV_TYPE] ?? 0,
                ]
            ]
        ];
    }
}
